<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Axios拦截器</title>
    <script src="vue.js"></script>
    <script src="axios.min.js"></script>
    <style>
        td {
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="getUserList()">获取所有用户信息</button>
        <div>
            <table border="1px" cellpadding="3px" cellspacing="2px">
                <tr>
                    <td>用户id</td>
                    <td>电话</td>
                    <td>密码</td>
                    <td>姓名</td>
                    <td>生日</td>
                    <td>创建日期</td>
                    <td>头像</td>
                    <td>是否可用</td>
                    <td>部门名称</td>
                </tr>
                <tr v-for="user in users">
                    <td>{{user.uid}}</td>
                    <td>{{user.telephone}}</td>
                    <td>{{user.password}}</td>
                    <td>{{user.realname}}</td>
                    <td>{{user.birthday}}</td>
                    <td>{{user.createdate}}</td>
                    <td>{{user.headimg}}</td>
                    <td>{{user.available}}</td>
                    <td>{{user.dept.deptname}}</td>
                </tr>
            </table>
        </div>
    </div>


</body>

</html>
<script>
    var vm = new Vue({
        el: "#app",
        data: {
            url: "http://localhost:8080/users",
            users: []
        },
        mounted() {
            axios.interceptors.request.use(function (config) {
                console.log("*******请求之前*********");
                config.headers["token"]="456789";
                console.log("***********************");
                return config;
            }, function (error) {
                return Promise.reject(error)
            });

            axios.interceptors.response.use(function (config) {
                console.log("*******响应之前*********");
                console.log(config.data.data.token);
                console.log("***********************");
                return config;
            }, function (error) {
                return Promise.reject(error)
            });

        },
        methods: {

            //获取所有用户信息
            getUserList() {
                axios.get(this.url).then(res => {
                    console.log(res);
                    this.users = res.data.data.users;
                }).catch(res => {
                    Promise.reject();
                })
            }
        },
    })
</script>